package Leetcode;

public class findMedianSortedArrays {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int len = nums1.length;
        int len2 = nums2.length;
        int[] nums = new int[len + len2];
        int i = 0, j = 0;
        int k = 0;
        while (i < len && j < len2){
            if (nums1[i] < nums2[j]){
                nums[k] = nums1[i];
                i++;
                k++;
            } else {
                nums[k] = nums2[j];
                j++;
                k++;
            }
        }
        if (i != len){
            while (i < len){
                nums[k] = nums1[i];
                i++;
                k++;
            }
        }
        if (j != len2){
            while (j < len2){
                nums[k] = nums2[j];
                j++;
                k++;
            }
        }
        return findMedian(nums);
    }
    public double findMedian(int[] nums){
        int len = nums.length;
        if (len == 0){
            return 0;
        }
        if (len == 1){
            return nums[0];
        }
        int left = 0, right= len - 1;
        int mid = left + (right - left) / 2;
        if (len % 2 == 0){
            return (double) (nums[mid] + nums[mid + 1]) / 2;
        } else {
            return nums[mid];
        }
    }
}
